package com.kaifamiao.www.Thread.do0927;

import java.time.LocalDateTime;

public class Cook {
    //面包
    private String name;

    private Cook(){}

    private static Cook cook = null;

    public static Cook getInstance(){
        if (cook == null){
            cook = new Cook();
        }
        return cook;
    }
    public synchronized void make(){
        if (name != null){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            name = LocalDateTime.now().toString();
            notifyAll();
            System.out.println("做了一个面包："+name);
        }
    }
    public synchronized void eat(){
        if (name == null){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println(Thread.currentThread().getName() + "吃了一个面包"+name);
            name = null;
            notifyAll();
        }
    }
}

